/*****************************************************************
 M3D Engine v0.1

 File: myd_camera.cpp
 Created: 11/08/09
*****************************************************************/

#include "myd_camera.h"

using namespace myd;

//-----------------------------------------------------------------
Camera::Camera(void)
{
	wn_v3pos.x		= 0.0f;   
	wn_v3pos.y		= 100.0f; 
	wn_v3pos.z		= -100.0f;
	
	wn_v3look.x		= 0.0f;  
	wn_v3look.y		= -1.0f; 
	wn_v3look.z		= 1.0f;
	
	wn_v3up.x		= 0.0f;	 
	wn_v3up.y		= 1.0f;	  
	wn_v3up.z		= 0.0f;
	
	wn_v3right.x	= 1.0f; 
	wn_v3right.y	= 0.0f; 
	wn_v3right.z	= 0.0f;

	wn_cameratype = ECT_AIR;
}

Camera::Camera(D3DXVECTOR3 v3pos)
{
	wn_v3pos = v3pos;
	wn_v3look.x = 0.0f;  wn_v3look.y = 0.0f; wn_v3look.z = 1.0f;
	wn_v3up.x = 0.0f;	 wn_v3up.y = 1.0f;	  wn_v3up.z = 0.0f;
	wn_v3right.x = 1.0f; wn_v3right.y = 0.0f; wn_v3right.z = 0.0f;

	wn_cameratype = ECT_AIR;
}

Camera::Camera(float xpos, float ypos, float zpos)
{
	wn_v3pos.x = xpos;   wn_v3pos.y = ypos;   wn_v3pos.z = zpos;
	wn_v3look.x = 0.0f;  wn_v3look.y = 0.0f; wn_v3look.z = 1.0f;
	wn_v3up.x = 0.0f;	 wn_v3up.y = 1.0f;	  wn_v3up.z = 0.0f;
	wn_v3right.x = 1.0f; wn_v3right.y = 0.0f; wn_v3right.z = 0.0f;

	wn_cameratype = ECT_AIR;
}

//-----------------------------------------------------------------
Camera::~Camera(void)
{
}
//-----------------------------------------------------------------
void Camera::Roll(float angle)
{
	D3DXMATRIX mat;
	
	//create rotation matrix on look axis
	D3DXMatrixRotationAxis(&mat, &wn_v3look, angle);

	//transform vector right using the matrix
	D3DXVec3TransformCoord(&wn_v3right, &wn_v3right, &mat);

	//transform vector up using matrix
	D3DXVec3TransformCoord(&wn_v3up, &wn_v3up, &mat);
}
//-----------------------------------------------------------------
void Camera::Pitch(float angle)
{
	D3DXMATRIX mat;

	//create rotation matrix on right axis
	D3DXMatrixRotationAxis(&mat, &wn_v3right, angle);

	//transform vector look using the matrix
	D3DXVec3TransformCoord(&wn_v3look, &wn_v3look, &mat);

	//transform vector up using matrix
	D3DXVec3TransformCoord(&wn_v3up, &wn_v3up, &mat);
}
//-----------------------------------------------------------------
void Camera::Yaw(float angle)
{
	D3DXMATRIX mat;

	if (wn_cameratype == ECT_GROUND)
	{
		wn_v3up.x = 0.0f; wn_v3up.y = 1.0f; wn_v3up.z = 0.0f;
		
		//rotation matrix around y axis
		D3DXMatrixRotationY(&mat, angle);
	}
	else if (wn_cameratype == ECT_AIR)
	{
		//rotation matrix around up
		D3DXMatrixRotationAxis(&mat, &wn_v3up, angle);

		//transform vector right using matrix
		D3DXVec3TransformCoord(&wn_v3right, &wn_v3right, &mat);

		//transform vector look using matrix
		D3DXVec3TransformCoord(&wn_v3look, &wn_v3look, &mat);
	}

}
//-----------------------------------------------------------------
void Camera::Walk(float value)
{
	if (wn_cameratype == ECT_AIR)
		wn_v3pos.y += wn_v3look.y * value;

	wn_v3pos.x += wn_v3look.x * value;
	wn_v3pos.z += wn_v3look.z * value;


}
//-----------------------------------------------------------------
void Camera::Strafe(float value)
{
	if (wn_cameratype == ECT_AIR)
		wn_v3pos.y += wn_v3right.y * value;

	wn_v3pos.x += wn_v3right.x * value;
	wn_v3pos.z += wn_v3right.z * value;

	//wn_v3look.x += wn_v3right.x * value;
	//wn_v3look.z += wn_v3right.z * value;
}
//-----------------------------------------------------------------
void Camera::Fly(float value)
{
	if (wn_cameratype == ECT_AIR)
	{
		wn_v3pos.x += wn_v3up.x * value;
		wn_v3pos.y += wn_v3up.y * value;
		wn_v3pos.y += wn_v3up.z * value;
	}
}
//-----------------------------------------------------------------
void Camera::setPos(D3DXVECTOR3 v3pos)
{
	wn_v3pos = v3pos;
}
//-----------------------------------------------------------------
void Camera::setLookPos(D3DXVECTOR3 v3lookpos)
{
	D3DXVECTOR3 v3;

	v3.x = v3lookpos.x - wn_v3pos.x;
	v3.y = v3lookpos.y - wn_v3pos.y;
	v3.z = v3lookpos.z - wn_v3pos.z;

	D3DXVec3Normalize(&v3, &v3);
	
	setLookAt(v3);
}
//-----------------------------------------------------------------
void Camera::setLookAt(D3DXVECTOR3 v3Look)
{
	wn_v3look = v3Look;
}
//-----------------------------------------------------------------
void Camera::Draw(Renderer *tm_renderer)
{
	D3DXMATRIX mat;
	D3DXVECTOR3 v3look = wn_v3pos + wn_v3look;
	D3DXMatrixLookAtLH(&mat, &wn_v3pos, &v3look, &wn_v3up);
	
	tm_renderer->setMatrixMode(VIEW);
	tm_renderer->loadIdentity();
	tm_renderer->setTransform(VIEW, mat);
}